'use strict';

/* Services */

angular.module('myApp.servicesUser', []).
        factory('serviceUser', [ 'serviceFirebaseUsers', function(firebaseUser) {
            return {
                email : null,
                password : null,
                points : 0,
                
                getData : function () {
                    return {
                        email : this.email,
                        password : this.password,
                        points : this.points
                    };
                },
                
                clear : function () {
                    this.email = null;
                    this.password = null;
                    this.points = null;
                },
                
                createAccount : function (email, password, callbackOk, callbackError) {
                    var t = this;
                    firebaseUser.createAccount({
                        'email' : email ,
                        'password' : password
                    }, function(error) {
                        t.email = email;
                        t.password = password;
                        callbackOk();
                    }, callbackError);
                },
                
                synchro : function(userData, refActive) {
                    if (userData) {
                        var changed = false;
                        if (userData.points) {
                            this.points = userData.points;
                            changed = true;
                        }
                        if (changed == true) {
                            var data = this.getData();
                            console.log('trying to change user in active section', data);
                            refActive.set(data, function (res) {
                                console.log('user in active section changed',res);
                            });
                            
                        }
                    }
                },
                
                login : function(email, password, callbackOk, callbackError, callbackInvitation) {
                    var t = this;
                    
                    firebaseUser.getUserByEmail(email, function(user, ref) {
                        
                        if (user != null && user.password == password) {
                            console.log('ok, user zalogowany');
                            
                            t.email = email;
                            t.password = password;
                            t.points = user.points || 0;
                            
                            firebaseUser.setUserActive(t.getData(),
                                    function(error, refActive) {
                                        if (error) {
                                            console.log('error when try to set user active', error);
                                            if (callbackError) callbackError(error);
                                        } else {
                                            console.log('zalogowany');
                                                                                       
                                            /**
                                             * listener of changes on user data
                                             */
                                            ref.on('value', function(snapshot) {
                                                var data = snapshot.val();
                                                t.synchro(data, refActive);
                                                console.log('user has been changed in firebase', data);
                                            });
                                            
                                            if (callbackOk) callbackOk();
                                        }
                                    },
                                    function(data) {
                                        console.log('function 5');
                                        // if changed data has some information
                                        if (data.invitedUser) {
                                            firebaseUser.clearUserInvitation(t.getData(), function () {
                                                if (callbackInvitation) callbackInvitation(data.invitedUser);
                                            });
                                        }
                                    }
                            );

                        } else {
                            console.log('bledne haslo badz email');
                            if (callbackError) callbackError();
                        }
                    });
                }              
            }
        }]);
